// $codepro.audit.disable numericLiterals
/**
 * prescription panel
 */
package edu.gatech.oad.doctorsorder;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.JButton;

import edu.gatech.oad.ui.UserPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * panel to edit prescription
 * 
 * @author chen
 * @version 1.0
 */
public class PrescriptionPanel extends JPanel {
	/**
	 * initialize variables
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * text area for medication
	 */
	private final JTextField txtMedic;

	/**
	 * text area for dosage
	 */
	private final JTextField txtDos;

	/**
	 * the user type that is allowed to edit the content;s
	 */
	private static final String[] PRIVILEGEUSRTYPE = {
			"edu.gatech.oad.user.Nurse", "edu.gatech.oad.user.Doctor" };

	/**
	 * Create the panel.
	 * 
	 * @param pres
	 *            the prescription to view/edit
	 * @param userPanel
	 *            panel to display on
	 */
	public PrescriptionPanel(final Prescription pres, UserPanel userPanel) {
		// set up UI
		final GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 200, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		final JPanel medication = new JPanel();
		final GridBagConstraints gbc_medication = new GridBagConstraints();
		gbc_medication.insets = new Insets(0, 0, 5, 0);
		gbc_medication.fill = GridBagConstraints.HORIZONTAL;
		gbc_medication.gridx = 0;
		gbc_medication.gridy = 0;
		add(medication, gbc_medication);
		medication.setLayout(new BoxLayout(medication, BoxLayout.X_AXIS));

		final JLabel lblMedication = new JLabel("Medication: ");
		medication.add(lblMedication);

		txtMedic = new JTextField();
		txtMedic.setText(pres.getMedication());
		medication.add(txtMedic);
		txtMedic.setColumns(10);

		final JPanel panel = new JPanel();
		final GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 1;
		add(panel, gbc_panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		final JLabel lblDosage = new JLabel("Dosage: ");
		panel.add(lblDosage);

		txtDos = new JTextField();
		txtDos.setText(pres.getDosage());
		panel.add(txtDos);
		txtDos.setColumns(10);

		final JPanel instruction = new JPanel();
		final GridBagConstraints gbc_instruction = new GridBagConstraints();
		gbc_instruction.insets = new Insets(0, 0, 5, 0);
		gbc_instruction.fill = GridBagConstraints.BOTH;
		gbc_instruction.gridx = 0;
		gbc_instruction.gridy = 2;
		add(instruction, gbc_instruction);
		instruction.setLayout(new BoxLayout(instruction, BoxLayout.X_AXIS));

		final JLabel lblInstruction = new JLabel("Instruction: ");
		instruction.add(lblInstruction);

		final JScrollPane scrollPane = new JScrollPane();
		instruction.add(scrollPane);

		final JEditorPane dtrpnInstr = new JEditorPane();
		dtrpnInstr.setText(pres.getInstructions());
		scrollPane.setViewportView(dtrpnInstr);

		final JPanel btnPanel = new JPanel();
		final GridBagConstraints gbc_btnPanel = new GridBagConstraints();
		gbc_btnPanel.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnPanel.gridx = 0;
		gbc_btnPanel.gridy = 3;
		add(btnPanel, gbc_btnPanel);

		final JButton btnSave = new JButton("save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// save current information when save button clicked
				pres.setMedication(txtMedic.getText());
				pres.setDosage(txtDos.getText());
				pres.setInstructions(dtrpnInstr.getText());

			}
		});
		btnPanel.add(btnSave);

		// check edit privilege
		txtMedic.setEditable(false);
		txtDos.setEditable(false);
		dtrpnInstr.setEditable(false);
		for (String tmp : PRIVILEGEUSRTYPE) {
			String className = userPanel.getUser().getClass().getName();
			if (className.equals(tmp)) {
				txtMedic.setEditable(true);
				txtDos.setEditable(true);
				dtrpnInstr.setEditable(true);
			}
		}
	}
}
